package com.jio.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jio.entity.Role;
import com.jio.entity.User;
import com.jio.service.RoleService;
import com.jio.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(HttpSession session){
        if(session.getAttribute("user")!=null){
            return "main";
        }
        return "redirect:/login.jsp";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpSession session, String username, String password){
        User login = userService.login(username, password);
        if(login==null){
            return "redirect:/login.jsp";
        }else{
            session.setAttribute("user",login);
            return "main";
        }
    }

    //退出登录
    @RequestMapping("/users/logout")
    public String logout(HttpSession session){
        session.invalidate();//销毁会话信息
        return "redirect:/login.jsp";
    }

    //查询用户
    @RequestMapping("/users/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        //通过定义参数获取请求的页数与每页条数
        //PageHelper提供静态方法startPage(页数，条数)
        //注意书写在service执行前
        PageHelper.startPage(page, limit);
        List<User> all = userService.findAll();
        //PageInfo代表页面详情 使用查询返回的数据创建
        //可以将pageInfo直接返回到页面进行使用
        PageInfo<User> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/users/users-list";
    }

    //跳转添加页面
    @RequestMapping(value = "/users/add",method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        List<Role> all = roleService.findAll();
        request.setAttribute("roleList",all);
        return "users/users-add";
    }

    //添加用户信息
    @RequestMapping(value = "/users/add",method = RequestMethod.POST)
    public String add(User user,int roleId){
        userService.add(user,roleId);
        return "redirect:/users/findAll";
    }

    //跳转修改页面
    @RequestMapping(value = "/users/edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        User byId = userService.findById(id);
        List<Role> all = roleService.findAll();
        request.setAttribute("users",byId);
        request.setAttribute("roleList",all);
        return "/users/users-edit";
    }

    @RequestMapping(value = "/users/edit",method = RequestMethod.POST)
    public String edit(User user,int roleId){
        userService.edit(user,roleId);
        return "redirect:/users/findAll";
    }

    //删除用户
    @RequestMapping("/users/delete")
    public String delete(int id){
        userService.delete(id);
        return "redirect:/users/findAll";
    }


}
